package com.huaxin.hxcloud.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.HxScStaionListBean;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.work.HxScStationInfoActivity;
import com.huaxindata.common.util.HxLogUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2018/5/18
 * @Desc 站点列表界面
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxScStationListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.toolbar)
    Toolbar            mToolbar;
    @BindView(R.id.recyclerview)
    RecyclerView       mRecyclerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @BindView(R.id.content)
    FrameLayout        mContent;
    private String                 mStationCode;
    private String                 mStitionName;
    private HxScStationListAdapter mHxScStationListAdapter;
    private              int mPage        = 0;
    private final static int TYPE_REFRESH = 1;
    private final static int TYPE_MORE    = 2;
    private              int mPageSize    = 10;

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (extras.containsKey(Constants.IK_STATION_CODE) && extras.containsKey(Constants.IK_STATION_NAME)) {
            mStationCode = extras.getString(Constants.IK_STATION_CODE);
            mStitionName = extras.getString(Constants.IK_STATION_NAME);
            HxLogUtils.i("stationCode---->" + mStationCode);
        }
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_station_list2;
    }

    @Override
    public void initView() {
        setSupportActionBar(mToolbar);
        mToolbar.setTitle(mStitionName);

        mHxScStationListAdapter = new HxScStationListAdapter(HxScStationListActivity.this);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mSwipeLayout.setOnRefreshListener(this);
        mHxScStationListAdapter.setOnLoadMoreListener(this, mRecyclerview);
        mRecyclerview.setAdapter(mHxScStationListAdapter);
    }

    @Override
    protected void requestData() {
        updateLoadStatus(LOADING);
        ApiRequsetData(TYPE_REFRESH, mPage);
        mHxScStationListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HxScStaionListBean.StationListBean stationListBean = mHxScStationListAdapter.getData().get(position);
                Intent intent = new Intent(HxScStationListActivity.this, HxScStationInfoActivity.class);
                intent.putExtra(Constants.IK_STATION_CODE, stationListBean.getStationCode());
                intent.putExtra(Constants.IK_STATION_TYPE, stationListBean.getType());
                startActivity(intent);
            }
        });

    }

    private void ApiRequsetData(final int refreshState, int pageNum) {
        Call<HxScStaionListBean> call = HxTransmitRequestHelper.getRequestApi().getScFindOrgStationByPage(mStationCode, pageNum, mPageSize);
        autoClose(call);
        call.enqueue(new Callback<HxScStaionListBean>() {
            @Override
            public void onResponse(Call<HxScStaionListBean> call, Response<HxScStaionListBean> response) {
                HxScStaionListBean staionListBean = response.body();
                if (staionListBean.isSuccessful()) {
                    if (staionListBean.getStationList() == null || staionListBean.getStationList().size() == 0) {
                        updateLoadStatus(LOAD_NO_DATA);
                        return;
                    }
                    updateLoadStatus(LOAD_SUCCESS);
                    switch (refreshState) {
                        case TYPE_REFRESH://下拉刷新
                            if (staionListBean.getStationList().size() == 0) {
                                mHxScStationListAdapter.getData().clear();
                            } else {
                                mHxScStationListAdapter.setNewData(staionListBean.getStationList());
                            }
                            mSwipeLayout.setRefreshing(false);
                            mHxScStationListAdapter.setEnableLoadMore(true);
                            mHxScStationListAdapter.loadMoreComplete();
                            break;
                        case TYPE_MORE://上拉加载更多
                            if (staionListBean.getStationList().size() == 0) {
                                mHxScStationListAdapter.loadMoreEnd(false);
                            } else {
                                mHxScStationListAdapter.setEnableLoadMore(true);
                                mHxScStationListAdapter.addData(staionListBean.getStationList());
                                mHxScStationListAdapter.loadMoreComplete();
                            }
                            mSwipeLayout.setEnabled(true);
                            break;
                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<HxScStaionListBean> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    @Override
    public void onRefresh() {
        mPage = 0;
        ApiRequsetData(TYPE_REFRESH, mPage);
    }

    @Override
    public void onLoadMoreRequested() {
        //少于一页的数量
        if (mHxScStationListAdapter.getData().size() < 10) {
            mHxScStationListAdapter.loadMoreEnd(true);
        } else {
            mPage++;
            //加载更多操作,下拉暂时不可用
            mSwipeLayout.setEnabled(false);
            ApiRequsetData(TYPE_MORE, mPage);
            Log.d(TAG, "onRefresh: ---加载更多->");
        }
    }

}
